import type { VbenFormProps, VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import { queryPage } from './api';
import { ref } from 'vue';
import { z } from '#/adapter/form';

// 操作id
export const idKey = ref('companyId');
export const handleId = ref('');

// 表格表单数据
export const formOptions: VbenFormProps = {
  schema: [
    {
      component: 'Input',
      defaultValue: '',
      fieldName: 'companyName',
      label: '机构名称',
    },
    {
      component: 'Input',
      defaultValue: '',
      fieldName: 'linkMan',
      label: '联系人',
    },
    {
      fieldName: 'companyState',
      label: '状态',
      component: 'Select',
      componentProps: {
        options: [
          { label: '已显示', value: 1 },
          { label: '已隐藏', value: 0 },
        ],
      },
    },
  ],
  wrapperClass: 'grid-cols-5',
};

// 表格数据
export const gridOptions: VxeTableGridOptions = {
  rowConfig: {
    height: 100,
  },
  columns: [
    { title: '序号', type: 'seq', width: 80 },
    { title: '机构名称', field: 'companyName', width: 160 },
    {
      title: '机构LOGO',
      field: 'companyLogo',
      align: 'center',
      cellRender: {
        name: 'CellImageViewer',
        props: { width: '60px', height: '60px' },
      },
      width: 120,
    },
    { title: '联系人', field: 'linkMan', width: 160 },
    { title: '联系电话', field: 'linkTel', width: 160 },
    { title: '机构介绍', field: 'businessScope', align: 'left' },
    {
      title: '状态',
      field: 'companyState',
      cellRender: {
        name: 'CellDict',
        props: { dictType: 'STATE' },
      },
      width: 120,
    },
    {
      fixed: 'right',
      slots: { default: 'action' },
      title: '操作',
      width: 160,
    },
  ],
  minHeight: '100%',
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues) => {
        let params = {
          page: page.currentPage,
          limit: page.pageSize,
          handlerName: 'baseCompanyListHandler',
          ...formValues,
        };
        return await queryPage(params);
      },
    },
    response: {
      result: 'data.records',
      total: 'data.total',
      list: 'data.records',
    },
  },
  pagerConfig: {
    enabled: true,
    pageSize: 5,
  },
  toolbarConfig: {
    custom: true,
    export: false,
    import: false,
    refresh: true,
    zoom: true,
  },
};

// 编辑弹窗表单配置
export const drawerFormSchema: VbenFormSchema[] = [
  {
    component: 'Input',
    defaultValue: '',
    fieldName: idKey.value,
    label: '机构id',
    dependencies: {
      show() {
        return false;
      },
      triggerFields: [''],
    },
  },
  {
    component: 'Input',
    componentProps: {
      placeholder: '请输入机构名称',
      allowClear: true,
      class: 'w-1/3',
    },
    fieldName: 'companyName',
    label: ' 机构名称',
    rules: 'required',
  },
  {
    component: 'ImageUpload',
    fieldName: 'companyLogo',
    help: '请上传机构LOGO',
    label: '机构LOGO',
    rules: z.string().min(1, { message: '请上传机构LOGO' }),
  },
  {
    component: 'Input',
    componentProps: {
      placeholder: '请输入联系人',
      allowClear: true,
      class: 'w-1/3',
    },
    fieldName: 'linkMan',
    label: '联系人',
    rules: 'required',
  },
  {
    component: 'Input',
    componentProps: {
      placeholder: '请输入联系电话',
      allowClear: true,
      class: 'w-1/3',
    },
    fieldName: 'linkTel',
    label: '联系电话',
    rules: 'required',
  },
  {
    component: 'Textarea',
    componentProps: {
      placeholder: '请输入机构介绍',
      allowClear: true,
    },
    fieldName: 'businessScope',
    label: '机构介绍',
    rules: 'required',
  },
  {
    component: 'TinymceEditor',
    fieldName: 'companyDesc',
    label: '机构简介',
    rules: 'required',
  },
];
